﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace lab3
{
    /// <summary>
    /// This class represents a student.
    /// </summary>
    /// <author>Donnie Prather gtg571m</author>
    public class Student
    {
        /// A list of all of the grades for this student
        private List<int> grades;
        /// The student's name
        private string name;
        /// The student's ID number
        private int id;

        [XmlAttribute]
        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public List<int> Grades
        {
            get { return grades; }
            set { grades = value; }
        }

        /// <summary>
        /// Creates a new student.
        /// </summary>
        /// <param name="name">The name of the new student</param>
        /// <param name="id">The student's ID number</param>
        public Student(string name, int id)
        {
            /// Set the new student's name
            this.name = name;
            /// Set the ID
            this.id = id;
            /// Create a list for grades
            grades = new List<int>();
        }

        /// <summary>
        /// Default constructor needed for xml serialization
        /// </summary>
        public Student()
        {
        }
        
        /// <summary>
        /// Add a new grade to the student's record.
        /// </summary>
        /// <author>Donnie Prather gtg571m</author>
        /// <param name="grade">New grade to add</param>
        public void AddGrade(int grade)
        {
            /// Add the new grade to the student's list of grades
            grades.Add(grade);
        }

        /// <summary>
        /// Compute a student's average
        /// </summary>
        /// <returns>The average</returns>
        public double Average()
        {
            /// Holds of the sum of all the student's grades
            int sumOfGrades = 0;
            /// How many grades the student has
            int numOfGrades = grades.Count;

            /// Iterate over each grade the student has
            foreach (int grade in grades)
            {
                /// Increment the sum with the current grade
                sumOfGrades += grade;
            }

            /// Check if the number of grades the student has is > 0
            /// to stop from dividing by 0.
            if (numOfGrades > 0)
            {
                /// return the average
                return (double) sumOfGrades / numOfGrades;
            }
            else
            {
                /// return 0 since the student has no grades
                return 0;
            }
        }

        /// <summary>
        /// Print out the student's name and their average.
        /// 
        /// Ex: Name: Bob   Average: 88
        /// </summary>
        /// <returns>A string with the student's name and average.</returns>
        public override string ToString()
        {
            /// return the student's name and average
            return "Name: " + name + "\tAverage: " + Average();
        }
    }
}
